<?php
class Event extends Publication{
	// Déclarations des données

	var $league = NULL;
	var $team1 = NULL;
	var $team2 = NULL;
	
	// Constructeur
	function Event($pId){
		global $wpdb;
		
		// Instanciation de parent
		parent::Publication("EVENT", $pId);
		
		$_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM events WHERE id = %d", $pId));		
		$this->league = $_row->fk_league;
		$this->team1 = $_row->team1;
		$this->team2 = $_row->team2;

	}
	
	// Déclarations des méthodes
	
	//======================
	// Création
	//======================
	
	function create(){
		global $wpdb;
		 
		$queryTable = 'events';
		$queryData = array( 
			'id' => $this->id, 
			'fk_league' => $this->league,
			'team1' => $this->team1,
			'team2' => $this->team2,
			'startdate' => $this->date,
			'starthour' => $this->hour
		);
		$queryDataFormat = array('%d','%d','%s','%s','%s','%s');
		
		$wpdb->insert( $queryTable, $queryData, $queryDataFormat);
	}
	
	//======================
	// Sauvegarde
	//======================
	
	function save(){
		global $wpdb;
		 
		$queryTable = 'events';
		$queryData = array( 
			'fk_league' => $this->league,
			'team1' => $this->team1,
			'team2' => $this->team2,
			'startdate' => $this->date,
			'starthour' => $this->hour
		);
		$queryDataFormat = array('%d','%s','%s','%s','%s');		
		$queryCondition = array("id" => $this->id);		
		$queryConditionFormat = array('%d');
		
		$wpdb->update( $queryTable, $queryData, $queryCondition, $queryDataFormat, $queryConditionFormat);
	}
	
	//======================
	// Get/Set
	//======================

	function getLeague(){
		return new League($this->league);
	}
	
	function setLeague($pLeague){
		$this->league = $pLeague;
	}
	
	function getTeam($pLocation){
		switch ($pLocation){
			case 'home' :
				return $this->team1;
				break;
			case 'away' :
				return $this->team2;
				break;
			default :
				return "undefined";
		}
	}
	
	function setTeam($pLocation, $pName){
		switch ($pLocation){
			case 'home' :
				$this->team1 = $pName;
				break;
			case 'away' :
				$this->team2 = $pName;
				break;
			default :
				return "error location";
		}
	}
	
	function getName(){
		return $this->team1." - ".$this->team2;
	}

	//======================
	// Récupération de la liste des bets disponible pour cet event
	//======================
	
	function getBets(){
		global $wpdb;
		
		return 
		$wpdb->get_results(
			$wpdb->prepare(
				// requete
				"SELECT id FROM bets WHERE fk_event = %d ORDER BY fk_bettype, tip",
				// parametres
				$this->id
			)
		);
	}
	
	function toHTML(){
		// representation d'un event dans le feed
	}

	function delete()
	{
	}

	/**
	 * 
	 * @param comment
	 */
	function addComment($comment)
	{
	}
}